//package com.qujia.station.service.impl;
//
//import cn.hutool.core.collection.CollectionUtil;
//import com.qujia.common.exception.BaseException;
//import com.qujia.common.utils.StringUtils;
//import com.qujia.station.domain.CardBatch;
//import com.qujia.station.domain.Enterprise;
//import com.qujia.station.domain.Station;
//import com.qujia.station.mapper.CardBatchMapper;
//import com.qujia.station.mapper.PhysicalCardMapper;
//import com.qujia.station.service.CardBatchService;
//import com.qujia.station.service.IEnterpriseService;
//import com.qujia.station.service.IStationService;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//
//import javax.annotation.Resource;
//import java.util.Arrays;
//import java.util.List;
//import java.util.Objects;
//import java.util.stream.Collectors;
//
//@Service("cardBatchService")
//@Slf4j
//public class CardBatchServiceImpl implements CardBatchService {
//
//    @Resource
//    private CardBatchMapper cardBatchMapper;
//
//    @Resource
//    private PhysicalCardMapper physicalCardMapper;
//
//    @Autowired
//    private IEnterpriseService enterpriseService;
//
//    @Autowired
//    private IStationService stationService;
//
//    @Override
//    public int saveCardBatch(CardBatch cardBatch) {
//        if(Objects.isNull(cardBatch)){
//            throw new BaseException("请求参数为空");
//        }
//        if(StringUtils.isEmpty(cardBatch.getBatchNo())){
//            throw new BaseException("请求参数没有批次号");
//        }
//        if(StringUtils.isEmpty(cardBatch.getPhysicalCardType())){
//            throw new BaseException("请求参数没有选择实体卡类型");
//        }
//        CardBatch newCardBatch = cardBatchMapper.selectByPrimaryKey(cardBatch.getBatchNo(),cardBatch.getPhysicalCardType());
//        if(Objects.nonNull(newCardBatch)){
//            throw new BaseException("批次号已存在");
//        }
//        // 定额卡需要一开始配置油企油站  储值卡不会
//        if(Objects.nonNull(cardBatch.getEnterpriseId())){
//            Enterprise enterprise = enterpriseService.selectEnterpriseById(cardBatch.getEnterpriseId());
//            cardBatch.setEnterpriseName(Objects.nonNull(enterprise) ? enterprise.getEnterpriseName() : null);
//        }
//        if(StringUtils.isNotEmpty(cardBatch.getStationId())){
//            cardBatch.setStationName(getStationName(cardBatch.getStationId()));
//        }
//
//        return cardBatchMapper.insertSelective(cardBatch);
//    }
//
//    private String getStationName(String stationIds){
//        String[] strings = stationIds.split(",");
//        String stationName = "";
//        for (String s : strings){
//            Station station = stationService.selectStationById(Long.valueOf(s));
//            stationName = stationName + station.getStationName() + ",";
//        }
//        stationName = stationName.substring(0,stationName.length() - 1);
//        return stationName;
//    }
//
//    @Override
//    public List<CardBatch> selectCardBatchList(CardBatch cardBatch) {
//        List<CardBatch> cardBatchList = cardBatchMapper.selectCardBatchList(cardBatch);
//        if(CollectionUtil.isEmpty(cardBatchList)){
//            return null;
//        }
//        for (CardBatch batch : cardBatchList) {
//            if(Objects.nonNull(batch)){
//                int num = physicalCardMapper.selectPhysicalCardNumByBatchId(batch.getId().toString());
//                batch.setTotalNum(num);
//            }
//        }
//        return cardBatchList;
//    }
//
//    @Override
//    public int updateCardBatch(CardBatch cardBatch) {
//        if(Objects.isNull(cardBatch)){
//            throw new BaseException("请求参数为空");
//        }
//        if(StringUtils.isEmpty(cardBatch.getBatchNo())){
//            throw new BaseException("请求参数没有批次号");
//        }
//        return cardBatchMapper.updateByPrimaryKeySelective(cardBatch);
//    }
//
//    @Override
//    public int deleteCardBatch(Long id) {
//        return cardBatchMapper.updateCardBatch(id);
//    }
//
//    @Override
//    public CardBatch selectCardBatchLastByType(CardBatch cardBatch) {
//        return cardBatchMapper.selectCardBatchLastByType(cardBatch.getPhysicalCardType());
//    }
//
//    @Override
//    public CardBatch selectCardBatchById(Long id) {
//
//        return cardBatchMapper.selectCardBatchById(id);
//    }
//
//    @Override
//    public CardBatch selectCardBatch(String batchNo, String physicalCardType) {
//        return cardBatchMapper.selectByPrimaryKey(batchNo,physicalCardType);
//    }
//
//}
